#!/bin/bash

if [ -f /data/updater.zip ]; then
 UPDATER=/data/updater.zip
 ISTAR=0
 F2FS=`unzip -l $UPDATER | grep rootfs.f2fs`
 if [ "$FS2FS" != "" ]; then
  FSTYPE=f2fs
 fi

else
 UPDATER=/data/updater.tar.gz
 ISTAR=1
fi

if [ "$FSTYPE" == "" ]; then
 FSTYPE=ext2
fi

if [ ! -f $UPDATER ]; then
 echo "$UPDATER does not exist, aborting"
 exit 1
fi

mount -o remount,rw /boot
if [ ! -f /boot/cmdline.txt ]; then
 echo "can't write to /boot/cmdline.txt, aborting"
 exit 1
fi

cd `dirname $0`
INACTIVE=`./partitions | awk '{print $2}'`

echo "unmounting $INACTIVE..."
umount /newroot 2>/dev/null

echo "extracting new firmware to $INACTIVE..."
if [ "$ISTAR" == "1" ]; then 
 gunzip -c $UPDATER | tar xOf - rootfs.$FSTYPE | dd of=$INACTIVE bs=1048576
else
 unzip -p $UPDATER rootfs.$FSTYPE | dd of=$INACTIVE bs=1048576
fi

echo "mounting new root file system..."
if [ ! -d /newroot ]; then
  mkdir /newroot
fi

echo "resizing new root file system..."
if [ "$FSTYPE" == "ext2" ]; then
 resize2fs $INACTIVE
else
 resize.f2fs $INACTIVE
fi

mount -t ext4 $INACTIVE /newroot
if [ -f /newroot/resize-me ]; then
  rm /newroot/resize-me
fi
VERSION=`cat /newroot/etc/hifiberry.version`

KERNELIMAGE=zImage
if [ -f /newroot/usr/lib/firmware/rpi/Image ]; then
  KERNELIMAGE=Image
fi

echo "extracting new kernel..."
if [ -f /newroot/usr/lib/firmware/rpi/$KERNELIMAGE ]; then
 echo "Using $KERNELIMAGE from new RPI firmware"
 cp /boot/$KERNELIMAGE /boot/$KERNELIMAGE.bak
 cp -rv /newroot/usr/lib/firmware/rpi/* /boot
else
 cd /tmp
 if [ "$ISTAR" == "1" ]; then
  gunzip -c $UPDATER | tar xf - $KERNELIMAGE updater.sh updater2.sh
 else
   unzip $UPDATER  $KERNELIMAGE updater.sh updater2.sh
 fi
 if [ ! -f $KERNELIMAGE ]; then
  echo "Could not find new kernel, aborting ..."
  exit 1
 else
  cp /boot/$KERNELIMAGE /boot/$KERNELIMAGE.bak
  mv $KERNELIMAGE /boot/$KERNELIMAGE 2>/dev/null
 fi
fi
echo "migrating SSH host keys..."
cp /etc/ssh/ssh_host* /newroot/etc/ssh

echo "migrating other configurations..."
for i in `cat /newroot/opt/hifiberry/etc/config-files`; do
  if [ -f $i ]; then
    DIR=`dirname $i`
    cd $DIR
    if [ ! -d /newroot/$DIR ]; then
      mkdir -p /newroot/$DIR
    fi
    echo $i
    cp $i /newroot/$DIR
  fi
  if [ -d $i ]; then
    DIR=`dirname $i`
    cd $DIR
    if [ ! -d /newroot/$DIR ]; then
      mkdir -p /newroot/$DIR
    fi
    echo "$i"
    cp -r $i /newroot/$DIR
  fi
done

echo "changing files on FAT partition..."
cd /boot
(cd /newroot/boot ; tar cf - *) | tar xvf -
cd /

echo "checking systemctl services..."
for i in `cat /opt/hifiberry/etc/services`; do
 systemctl is-active --quiet $i
 if [ "$?" == "0" ]; then
   echo "$i: enabled"
   if [ ! -f "/newroot/etc/systemd/system/multi-user.target.wants/$i" ]; then
     echo "enabling..."
     ln -s ../../../../usr/lib/systemd/system/$i /newroot/etc/systemd/system/multi-user.target.wants/$i
   fi
 else
   echo "$i: disabled"
   if [ -f "/newroot/etc/systemd/system/multi-user.target.wants/$i" ]; then
     echo "disabling..."
     rm /newroot/etc/systemd/system/multi-user.target.wants/$i
   fi
 fi
done

echo "looking for update script..."
if [ -f /newroot/tmp/updater.sh ]; then
 cp /newroot/tmp/updater.sh /tmp/updater.sh
elif [ -f /newroot/updater.sh ]; then
 cp /newroot/updater.sh /tmp/updater.sh
fi
cd /tmp
if [ ! -f updater.sh ]; then
 echo "no post-config update script found, continuing..."
else
 echo "starting upgrade script..."
 chmod +x ./updater.sh
 ./updater.sh
fi

echo "storing version number of previous release..."
cp /etc/hifiberry.version /newroot/etc/hifiberry.version.previous
sync

echo "unmounting $INACTIVE..."
umount $INACTIVE
fsck.ext2 -p -f -v $INACTIVE
RES=$?
if [ "$RES" != "0" ] && [ "$RES" != "1" ]; then
 echo "could file system is corrupted, not switching to new version..."
 exit
fi

if [ "$1" == "--simulate" ]; then
 echo "not switching to new version..."
 exit
fi

echo "switching root file system to inactive partition..."
echo "was:     " `cat /boot/cmdline.txt`
QUOTED_INACTIVE=$(sed 's/[\/&]/\\&/g; s/$/\\/' <<< "$INACTIVE")
CMDLINE=`cat /boot/cmdline.txt | sed "s/root\=[a-zA-Z0-9\/]*/root=$QUOTED_INACTIVE /"`
echo "becomes: " $CMDLINE
echo "$CMDLINE" > /boot/cmdline.txt

if [ "$1" == "--reboot" ]; then
 echo "removing updater..."
 rm $UPDATER
 echo "rebooting..."
 sync
 reboot
else
 echo "Version $VERSION installed. You need to reboot to activate it"
fi

